package com.yuyou.tas.lesson.controller;

import com.yuyou.tas.lesson.service.IBaseSectionService;
import com.yuyou.tas.lesson.vo.resp.SubjectSections;
import com.yuyou.tas.service.api.lesson.dto.DtoBaseSection;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.PageParams;
import com.yuyou.tas.util.bean.Pagination;
import com.yuyou.tas.util.bean.ReturnResult;
import com.yuyou.tas.util.enums.admin.AuthUserEnums;
import com.yuyou.tas.util.enums.lesson.LessonBaseEnums;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value="BaseSectionController",description="年级操作接口")
@Validated
@RestController
@RequestMapping(value="/api/BaseSection")
public class BaseSectionController extends BaseController {

    @Autowired
    private IBaseSectionService service;

    /*@Autowired
    private OrganLimitService organLimitService;


    @ApiOperation(value="添加年级")
    @RequestMapping(value="/addSection",method = RequestMethod.POST)
    public ReturnResult addSection(@Validated @ApiParam(value = "年级名称")@NotBlank(message = "年级名称不能为空")@RequestParam("name") String name,
                                  HttpServletRequest request){
        LoginRedisObj user = this.getUserObj(request);
        ResBoolSimpleInfo info = service.addSection(name ,user.getIsSystem()? CommEnums.standtype.standard.getValue():CommEnums.standtype.defind.getValue() ,
                                                    null, user,user.getOrganizationId());
        return ReturnResult.returnRes(info.isSuccess() , info.getMessage() , null);
    }

    @ApiOperation(value="删除年级")
    @RequestMapping(value="/delSection",method = RequestMethod.POST)
    public ReturnResult delSection(@Validated @ApiParam(value = "ID")@NotNull(message = "ID不能为空")@RequestParam(value = "id") Long id,
                                   HttpServletRequest request) throws Exception {
        LoginRedisObj user = this.getUserObj(request);
        if(!user.getIsSystem()) {
            organLimitService.checkOrganByOrganId(user.getOrganizationId(), id, OrganLimitEnums.type.base_section);
        }
        ResBoolSimpleInfo info = service.delSection(id);
        return ReturnResult.returnRes(info.isSuccess() , info.getMessage() , null);
    }


    @ApiOperation(value="通过ID获取年级信息")
    @RequestMapping(value="/getBaseSection",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<BaseSection> getBaseSection(@Validated @NotNull(message = "ID不能为空")@ApiParam(value = "ID" , required = true) @RequestParam(value = "id") Long id,
                                                    HttpServletRequest request) throws Exception {
        LoginRedisObj user = this.getUserObj(request);
        if(!user.getIsSystem()) {
            organLimitService.checkOrganByOrganId(user.getOrganizationId(), id, OrganLimitEnums.type.base_section);
        }
        ResBoolSimpleInfo info =  service.getSectionByID(id);
        return ReturnResult.returnRes(info.isSuccess() , info.getMessage() , info.getData());
    }

    @ApiOperation(value="修改年级")
    @RequestMapping(value="/updateSection",method = RequestMethod.POST)
    public ReturnResult updateSection( @Validated @ApiParam(value = "ID")@NotNull(message = "ID不能为空")@RequestParam(value = "id") Long id,
                                     @Validated @ApiParam(value = "年级名称")@NotBlank(message = "年级名称不能为空")@RequestParam("name") String name,
                                       HttpServletRequest request) throws Exception {
        LoginRedisObj user = this.getUserObj(request);
        if(!user.getIsSystem()) {
            organLimitService.checkOrganByOrganId(user.getOrganizationId(), id, OrganLimitEnums.type.base_section);
        }
        ResBoolSimpleInfo info = service.updateSection(name,user.getIsSystem()?CommEnums.standtype.standard.getValue():CommEnums.standtype.defind.getValue() ,null, id);
        return ReturnResult.returnRes(info.isSuccess() , info.getMessage() , null);
    }*/

    @ApiOperation(value = "查询年级")
    @RequestMapping(value = "/findBaseSectionPage" ,method = {RequestMethod.GET,RequestMethod.POST})
    public ReturnResult<DtoBaseSection> findBaseSectionPage(HttpServletRequest request,
                                                          @Validated @ModelAttribute PageParams pageParams,
                                                          @ApiParam(value = "是否通过授课班级获取：1、是，0或不传、否")@RequestParam(value = "tag",required = false) Integer tag,
                                                          @ApiParam(value = "年级名称")@RequestParam(value = "name",required = false) String name,
                                                          @ApiParam(value = "科目IDs")@RequestParam(value = "subjectId",required = false) String[] subjectId){
        Map<String, Object> conditions = new HashMap<>();
        Pagination<DtoBaseSection> queryPgn = new Pagination<>();
        LoginRedisObj user = this.getUserObj(request);

        if(tag != null && tag == 1){//备课、上课页面-通过班级获取
            List<DtoBaseSection> list = service.getUserSection(user.getUserId() , subjectId);
            queryPgn.setPageParams(pageParams);
            queryPgn.setTotal(list.size());
            queryPgn.toBePage();
            queryPgn.setList(list);
            return ReturnResult.success(queryPgn);
        }else if(!user.getIsManager() && user.getUserType().equals(AuthUserEnums.type.teaching.getValue())){//教研
            List<DtoBaseSection> list = service.getTeachingSection(user.getUserId() , subjectId);
            queryPgn.setPageParams(pageParams);
            queryPgn.setTotal(list.size());
            queryPgn.toBePage();
            queryPgn.setList(list);
            return ReturnResult.success(queryPgn);
        }



        conditions.put("isUse", LessonBaseEnums.isEnableed.enabl.getValue());
        conditions.put("isStan", 1);//获取标准数据

        if(subjectId != null && subjectId.length > 0){
            conditions.put("subjectId", subjectId);
        }
        /*if(!user.getIsManager()){//获取用户年级权限
            conditions.put("sectionIDs", user.getBaseSectionIds());
        }*/

        if(!StringUtils.isEmpty(name)){
            conditions.put("name", name.trim());
        }

//            conditions.put("organ_id", user.getOrganizationId());//默认查询个人所在机构的校区
        queryPgn.setConditions(conditions);
        queryPgn.setPageParams(pageParams);

        int total = service.findSectionCount(queryPgn);
        queryPgn.setTotal(total);
        queryPgn.toBePage();
        if(total > 0){
            List<DtoBaseSection> list = service.findSectionPageList(queryPgn);
            queryPgn.setList(list);
        }
        return ReturnResult.success(queryPgn);
    }

    @ApiOperation(value = "通过科目获取年级")
    @RequestMapping(value = "/findBaseSectionBySubject" ,method = {RequestMethod.GET,RequestMethod.POST})
    public ReturnResult<List<SubjectSections>> findBaseSectionBySubject(HttpServletRequest request,
                                                                        @ApiParam(value = "是否通过授课班级获取：1、是，0或不传、否")@RequestParam(value = "tag",required = false) Integer tag,
                                                                        @ApiParam(value = "科目IDs")@RequestParam(value = "subjectId",required = false) String[] subjectId) {
        Map<String, Object> conditions = new HashMap<>();
        LoginRedisObj user = this.getUserObj(request);
        if(!user.getIsManager() && tag != null && tag == 1){//备课、上课页面-通过班级获取
            List<SubjectSections> list = service.getUserSectionMap(user.getUserId() , subjectId);
            return ReturnResult.success(list);
        }else if(!user.getIsManager() && user.getUserType().equals(AuthUserEnums.type.teaching.getValue())){//教研
            List<SubjectSections> list = service.getTeachingSectionMap(user.getUserId() , subjectId);
            return ReturnResult.success(list);
        }

        conditions.put("isUse", LessonBaseEnums.isEnableed.enabl.getValue());
        conditions.put("isStan", 1);//获取标准数据

//            conditions.put("organ_id", user.getOrganizationId());//默认查询个人所在机构的校区
        if(subjectId != null && subjectId.length > 0){
            conditions.put("subjectIds", subjectId);
        }
        if(!user.getIsManager()){//获取用户年级权限
            conditions.put("sectionIDs", user.getBaseSectionIds());
        }

        List<SubjectSections> list = service.findBySubject(conditions);
        return ReturnResult.success(list);
    }
}
